/*
 * Copyright 北京航空航天大学  @ 2015 版权所有
 */
package com.buaa.edu.leetcode.algorithm.twopoint;

import java.util.Arrays;

/**
 * <p>
 * 有序数组元素去重 结果返回长度。 
 * 每个元素重复出现1次
 * For example, Given input array nums = [1,1,2],
 * Your function should return length = 2,
 * </p>
 * 
 * @author towan
 * @email tongwenzide@163.com
 * @time 2015年5月31日
 */
public class RemoveDuplicatesFromSortedArray {

public int removeDuplicates(int[] nums) {
    if(nums==null || nums.length<=1){
         return nums==null?0:nums.length;
     }   
     int len = nums.length;
    
     int first = 0;
     int second ;
     //second在前面走
     for(second=1;second<len;second++){
         if(nums[first]!=nums[second]){
             nums[++first] = nums[second];
         }
     }
     return first+1;
    }

    public static void main(String[] args) {
        RemoveDuplicatesFromSortedArray duplicatesFromSortedArray = new RemoveDuplicatesFromSortedArray();
        int[] nums = {1,1,2,3,3};
        System.out.println(duplicatesFromSortedArray.removeDuplicates(nums ));
        System.out.println(Arrays.toString(nums));
    }
}
